﻿using System;
using System.IO;
namespace CommandLib
{
    /// <summary>
    /// 异步日志
    /// </summary>
    public sealed class AsyncLoger
    {

        private static Action<object> ExecLog = (f) =>
        {
            Loger.Log(f.ToJsonString());
        };

        private static Action<object> FuncExecLog = (f) =>
        {
            Loger.Log(f.ToJsonString(), "ErrorLog");
        };
        private static Action<object> FuncExecLogFile = (f) =>
         {
             Loger.Log(f.ToJsonString(), "ApiErrorLog");
         };

        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="o">日志内容</param>
        /// <param name="files">自定义文件夹</param>
        private static void Log(object o, string files = null)
        {

            BackTaskListRun.AddFunc(delegate
            {
                if (string.IsNullOrEmpty(files))
                {
                    Loger.Log(o.ToJsonString());
                }
                else
                {
                    Loger.Log(o.ToJsonString(), files);
                }

            }, o);
        }

        /// <summary>
        /// 添加日志记录
        /// </summary>
        /// <param name="oc">日志对象</param>
        public static void AddLog(object oc)
        {

            BackTaskListRun.AddFunc(ExecLog, oc);

        }
        /// <summary>
        /// 添加异常日志记录
        /// </summary>
        /// <param name="oc">日志对象</param>
        public static void AddErrorLog(object oc)
        {

            BackTaskListRun.AddFunc(FuncExecLog, oc);

        }

        /// <summary>
        /// 添加Api异常日志记录
        /// </summary>
        /// <param name="oc">日志对象</param>
        /// <param name="files">设置根日录文件夹</param>
        public static void AddApiErrorLog(object oc)
        {
            BackTaskListRun.AddFunc(FuncExecLogFile, oc);
        }
    }

    public class Loger
    {
        private static readonly string methpath = AppDomain.CurrentDomain.BaseDirectory + "{0}/";
        private static object files = new object();

        /// <summary>
        /// 日志记录（将文件放在根目录的Log文件夹内）
        /// </summary>
        /// <param name="value">内容</param>
        public static void Log(string value)
        {
            lock (files)
            {
                StreamWriter sw = null;
                try
                {
                    var pathsite = string.Format(methpath, "Log");
                    string filename = DateTime.Now.ToString("yyy-MM-dd") + ".log";
                    if (!Directory.Exists(pathsite))
                    {
                        Directory.CreateDirectory(pathsite);
                    }
                    string path = pathsite + filename;
                    if (!File.Exists(path))
                    {
                        sw = File.CreateText(path);
                    }
                    else
                    {
                        sw = File.AppendText(path);
                    }
                    string logtxt = string.Format("时间：{0}\r\n内容：{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), value);
                    sw?.WriteLine(logtxt);

                }
                catch (Exception)
                {

                }
                finally
                {
                    sw?.Close();
                    sw?.Dispose();
                }

            }
        }

        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="value">日志内容</param>
        /// <param name="files">文件夹名</param>
        public static void Log(string value, string files)
        {
            lock (files)
            {
                StreamWriter sw = null;
                try
                {
                    var pathsite = string.Format(methpath, files);
                    string filename = DateTime.Now.ToString("yyy-MM-dd") + ".log";
                    if (!Directory.Exists(pathsite))
                    {
                        Directory.CreateDirectory(pathsite);
                    }
                    string path = pathsite + filename;
                    if (!File.Exists(path))
                    {
                        sw = File.CreateText(path);
                    }
                    else
                    {
                        sw = File.AppendText(path);
                    }
                    string logtxt = string.Format("时间：{0}\r\n内容：{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), value);
                    sw?.WriteLine(logtxt);

                }
                catch (Exception)
                {

                }
                finally
                {
                    sw?.Close();
                    sw?.Dispose();
                }

            }
        }

    }
}
